package cn.edu.neusoft.amicooldemo.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.edu.neusoft.amicooldemo.R;
import cn.edu.neusoft.amicooldemo.ViewtwareActivity;
import cn.edu.neusoft.amicooldemo.adapter.CTwareAdapter;
import cn.edu.neusoft.amicooldemo.bean.CollectResult;
import cn.edu.neusoft.amicooldemo.bean.TwareBean;
import cn.edu.neusoft.amicooldemo.factory.CollectFactory;
import cn.edu.neusoft.amicooldemo.iface.CollectListListener;
import cn.edu.neusoft.amicooldemo.iface.OnRecyclerViewItemClickListener;


/**
 * A simple {@link Fragment} subclass.
 */
public class CTwareFragment extends Fragment {


    private RecyclerView recyclerView;

    private List<CollectResult<TwareBean>> list;
    protected CTwareAdapter adapter;
    private static int cur_page=1;private int lastVisibleItemPosition;
    private static String mod;
    private String SessionID;
    private SharedPreferences sp;
    private CollectListListener<TwareBean> listener;

    public CTwareFragment() {
        // Required empty public constructor

        mod = "tware";
        list = new ArrayList<CollectResult<TwareBean>>();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_tware, container, false);
        sp = getActivity().getSharedPreferences("amicool_user", Context.MODE_PRIVATE);
        SessionID=sp.getString("SessionID","dfrcg3211kjb1vbe3ibgftkfa6");
        recyclerView = (RecyclerView) view.findViewById(R.id.tab_tware_list);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new CTwareAdapter(getActivity(),list);
        adapter.setOnItemClickListener(new CTwareAdapter.OnItemClickListener(){

            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getActivity(), ViewtwareActivity.class);
                intent.putExtra("resid",list.get(position).getResid());
                intent.putExtra("userid",list.get(position).getUserid());
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

        recyclerView.setAdapter(adapter);

        listener = new CollectListListener<TwareBean>() {

            @Override
            public void onResponse(List<CollectResult<TwareBean>> l) {
                list.removeAll(l);
                list.addAll(0,l);
                adapter.setList(list);
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getActivity(),"fail:"+msg,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void loadPreData(List<CollectResult<TwareBean>> l) {

            }

            @Override
            public void loadNextData(List<CollectResult<TwareBean>> l) {
                list.addAll(l);
                adapter.setList(list);
            }
        };
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == adapter.getItemCount()) {
                    cur_page+=1;
                    CollectFactory.getInstance().getResultList(mod,cur_page,SessionID,listener);

                }


            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();;

            }
        });
        if(cur_page==1)
        CollectFactory.getInstance().getResultList(mod,cur_page,SessionID,listener);
        return view;
    }

}
